package service;

import dao.UserDAO;
import domain.User;
import ex.DatabaseException;
import ex.RegistrationFailedException;

public class RatingService {

	UserDAO userDao=new UserDAO();
	public void rateUser(User user, int rate) throws DatabaseException{
		long rateCount=user.getPopularity();
		double newRate=2;
		if(rateCount==0){
			newRate=rate;
		}
		else{
			double oldRate=user.getRating();
			double totalRate=oldRate*rateCount;
			
			totalRate+=rate;
			rateCount++;
			
			newRate=totalRate/rateCount;			
		}
		
		
		user.setRating(newRate);
		user.setPopularity(rateCount);
		
		userDao.editUser(user);
	}
}
